package com.huike.web.controller.system;

import com.huike.clues.service.ISysDeptService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author EthanRuins
 * @date 2023/7/11
 * @apiNote
 */
@RestController
@RequestMapping("/system/dept")
@AllArgsConstructor
@Api(tags = "部门管理相关接口")
public class SysDeptController extends BaseController {
    private final ISysDeptService deptService;

    /**
     * 添加部门的同时添加员工
     * @param sysDept 新部门数据
     * @return success error
     */
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增部门")
    public AjaxResult<Object> add(@RequestBody SysDept sysDept) {
        String deptName = sysDept.getDeptName();
        if (deptName.length() < 1 || deptName.length() > 12) {
            return AjaxResult.error("添加失败，部门名称长度不合法");
        }
        if (deptService.checkNameUnique(deptName)) {
            return AjaxResult.error("添加失败，部门名称已存在");
        }
        return toAjax(deptService.add(sysDept));
    }

    /**
     * 修改部门数据
     * @param sysDept 新部门数据
     * @return success error
     */
    @PreAuthorize("@ss.hasPermi('system:dept:update')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "更新部门")
    public AjaxResult<Object> update(@RequestBody SysDept sysDept) {
        String deptName = sysDept.getDeptName();
        if (deptName.length() < 1 || deptName.length() > 12) {
            return AjaxResult.error("更新失败，部门名称长度不合法");
        }
        return toAjax(deptService.update(sysDept));
    }

    /**
     * 删除部门数据
     * @param deptId 部门id
     * @return success error
     */
    @PreAuthorize("@ss.hasPermi('system:dept:delete')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    @ApiOperation(value = "删除部门")
    public AjaxResult<Object> delete(@PathVariable Long deptId) {
        return toAjax(deptService.delete(deptId));
    }

    /**
     * 根据ID获取部门信息
     * @param deptId 部门ID
     * @return 部门信息
     */
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @GetMapping("/{deptId}")
    @ApiOperation(value = "根据ID查询部门")
    public AjaxResult<SysDept> getById(@PathVariable Long deptId) {
        SysDept dept = deptService.getById(deptId);
        return AjaxResult.success(dept);
    }

    /**
     * 获取部门信息列表
     * @param dept 查询条件
     * @return 部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表")
    public AjaxResult<List<SysDept>> list(SysDept dept) {
        startPage();
        List<SysDept> list = deptService.getDeptList(dept);
        return AjaxResult.success(list);
    }

    /**
     * 查询部门列表（排除指定部门结点）
     * @param deptId 指定部门结点的ID
     * @return 部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:exclude')")
    @GetMapping("/list/exclude/{deptId}")
    @ApiOperation(value = "查询部门列表（排除指定部门结点）")
    public AjaxResult<List<SysDept>> excludeList(@PathVariable Long deptId) {
        startPage();
        List<SysDept> list = deptService.excludeList(deptId);
        return AjaxResult.success(list);
    }

    /**
     * 获取部门下拉树列表
     * @return 部门列表
     */
    @PreAuthorize(value = "@ss.hasPermi('system:dept:treeselect')")
    @GetMapping("/treeselect")
    @ApiOperation(value = "获取部门下拉树列表")
    public AjaxResult<List<TreeSelect>> treeselect() {
        List<TreeSelect> list = new ArrayList<>();
        list.add(deptService.treeSelect());
        return AjaxResult.success(list);
    }

    /**
     * 获取部门及人员
     * @return 部门列表
     */
    @PreAuthorize(value = "@ss.hasPermi('system:dept:treeAndUser')")
    @GetMapping("/treeAndUser")
    @ApiOperation(value = "获取部门及人员列表")
    public AjaxResult<List<TreeSelect>> treeAndUser() {
        List<TreeSelect> list = new ArrayList<>();
        list.add(deptService.treeSelect());
        return AjaxResult.success(list);
    }
}
